package com.ruoyi.framework.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
 
 
//@Configuration
//@MapperScan(basePackages  = MasterDbConfig.PACKAGE , sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDbConfig {
     // 精确到 master 目录，以便跟其他数据源隔离
    static final String PACKAGE = "com.ruoyi.**.mapper,com.edu.base.umsMember.**.mapper";
    private static final String MAPPER_LOCATION = "classpath*:mapper/master/**/*.xml";
    private static final String DOMAIN_PACKAGE = "com.edu.base.umsMember,com.edu.base.rest,com.ruoyi";
    
    @Autowired
    private Environment env;
 
    @Value("${spring.datasource.druid.master.url}")
    private String dbUrl;
 
    @Value("${spring.datasource.druid.master.username}")
    private String username;
 
    @Value("${spring.datasource.druid.master.password}")
    private String password;
 
    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;
 
 
    @Bean(name="masterDataSource")   //声明其为Bean实例
    @Primary  //在同样的DataSource中，首先使用被标注的DataSource
    public DataSource masterDataSource() {
        DruidDataSource datasource = new DruidDataSource(); 
        datasource.setUrl(dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName); 
        return datasource;
    }
 
    @Bean(name = "masterTransactionManager")
    @Primary
    public DataSourceTransactionManager masterTransactionManager() {
        return new DataSourceTransactionManager(masterDataSource());
    }
  
    @Bean(name = "masterSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
            throws Exception { 
    	 final MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();// new SqlSessionFactoryBean();
         sessionFactory.setDataSource(masterDataSource);
         //添加一行如下代码
         VFS.addImplClass(SpringBootVFS.class);
         sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                 .getResources(MAPPER_LOCATION));
         String typeAliasesPackage = MyBatisConfig.setTypeAliasesPackage(DOMAIN_PACKAGE);
         sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
         String configLocation = env.getProperty("mybatis.configLocation"); 
         sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));

         //mybatis 数据库字段与实体类属性驼峰映射配置
         sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
         return sessionFactory.getObject();
    }
 
    
}
